package com.yq.service.impl;

import com.sun.org.apache.regexp.internal.REUtil;
import com.yq.mapper.AdminMapper;
import com.yq.model.bo.AddAdminBO;
import com.yq.model.bo.AdminInfoBO;
import com.yq.model.bo.AdminLoginBO;
import com.yq.model.po.Admin;
import com.yq.model.vo.AdminInfoVO;
import com.yq.service.AdminService;
import com.yq.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminServiceImpl implements AdminService {


    @Override
    public int login(AdminLoginBO adminLoginBO) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        Admin admin = new Admin();
        admin.setUsername(adminLoginBO.getEmail());
        admin.setPassword(adminLoginBO.getPwd());
        int count = mapper.queryCountByUsernameAndPassword(admin);
        sqlSession.commit();
        sqlSession.close();
        if (count==1){
            return 200;
        }
        return 404;
    }

    @Override
    public List<AdminInfoVO> queryAllAdmin() {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<AdminInfoVO> adminInfoVOList = mapper.queryAllAdminInfo();
        return adminInfoVOList;
    }

    @Override
    public int addAdmin(AddAdminBO addAdminBO) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        Admin admin = new Admin();
        admin.setUsername(addAdminBO.getEmail());
        admin.setPassword(addAdminBO.getPwd());
        admin.setNickname(addAdminBO.getNickname());
        int count  = 0;
        try {
            count  = mapper.addAdmin(admin);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
        }
        sqlSession.close();
        if (count==1){
            return 200;
        }
        return 404;
    }

    @Override
    public AdminInfoVO queryById(int id) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        AdminInfoVO adminInfoVO =mapper.queryById(id);
        sqlSession.commit();
        sqlSession.close();
        return adminInfoVO;
    }

    @Override
    public int updateAdminInfo(AdminInfoBO adminInfoBO) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        Admin admin = new Admin();
        admin.setId(adminInfoBO.getId());
        admin.setUsername(adminInfoBO.getEmail());
        admin.setNickname(adminInfoBO.getNickname());
        admin.setPassword(adminInfoBO.getPwd());
        int count  = 0;
        try {
            count  = mapper.updateAdminInfo(admin);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
        }
        if (count==1){
            return 200;
        }
        return 404;
    }

    @Override
    public int deleteAdminById(int id) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int count  = 0;
        try {
            count = mapper.deleteAdminById(id);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
        }
        if (count==1){
            return 200;
        }
        return 404;

    }

    @Override
    public List<AdminInfoVO> getSearchAdmins(AdminInfoBO adminInfoBO) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        Admin admin = new Admin();
        admin.setUsername(adminInfoBO.getEmail());
        admin.setNickname(adminInfoBO.getNickname());
        List<AdminInfoVO> list = mapper.getSearchAdminsByUsernameOrNickname(admin);
        return list;
    }
}
